package com.example.teadoctor;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Objects;

public class ConsultFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.page_consult,container,false);
        // 获取TeaDBHelper对象
        TeaDBHelper dbHelper = new TeaDBHelper(getActivity());

        // 获取SQLiteDatabase对象
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        String query = "SELECT * FROM tea_info";
        Cursor cursor = db.rawQuery(query, null);
        ArrayList<String> dataList = new ArrayList<>();
        while(cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String image = cursor.getString(cursor.getColumnIndex("image"));
            String method = cursor.getString(cursor.getColumnIndex("method"));
            String storage = cursor.getString(cursor.getColumnIndex("storage"));
            String record = id + "   " + name+ " " + image + "  做法：" + method + "  存储方法" + storage;
            dataList.add(record);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(Objects.requireNonNull(getActivity()), android.R.layout.simple_list_item_1, dataList);
        ListView listView = view.findViewById(R.id.Pst_my_consult);
        listView.setAdapter(adapter);
        // 关闭结果集和数据库连接
        cursor.close();
        db.close();

        return view;
    }
}
